package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.admin.service.IRemarkService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserRemarkLogMapper;
import com.ee.chat.common.entity.AdminUser;
import com.ee.chat.common.entity.UserRemarkLog;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author AppiJ
 * @since 2020-10-10
 */
@Service
public class RemarkServiceImpl implements IRemarkService {

    @Resource
    UserRemarkLogMapper remarkLogMapper;
    @Resource
    IOperationService operationService;

    @Override
    public Result list(HttpServletRequest request, Integer userId, String time, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(userId == null && StringUtil.isEmpty(time)){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/operate/remark/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = remarkLogMapper.list(p, userId, beginTime, endTime);
        List<Map<String, Object>> maps = iPage.getRecords();

        data.put("datas", maps);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取用户备注信息数据");

        return Result.returnSuccess(data);
    }

    @Override
    public Result toEdit(HttpServletRequest request, Integer id) throws Exception {
        Map<String, Object> data = new HashMap<>();

        UserRemarkLog remarkLog = remarkLogMapper.selectById(id);
        data.put("remarkLog", remarkLog);

        return Result.returnSuccess(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result edit(HttpServletRequest request, UserRemarkLog remarkLog) throws Exception {
        HttpSession session = request.getSession(true);
        AdminUser adminUser = (AdminUser) session.getAttribute("adminUser");

        remarkLog.setAdminId(adminUser.getId());
        remarkLogMapper.updateById(remarkLog);

        operationService.addOperationLog(request, "修改备注记录，id：" + remarkLog.getId());

        return Result.returnSuccess(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result del(HttpServletRequest request, List<Integer> ids) throws Exception {

        remarkLogMapper.delRemarkById(ids);

        operationService.addOperationLog(request, "删除用户备注记录，ids：" + ids);

        return Result.returnSuccess(null);
    }

}
